﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OAuth2.Framework
{
    /// <summary>
    /// Error Info
    /// </summary>
    public class ErrorResponse
    {
        /// <summary>
        /// REQUIRED. A single error code from the following:
        ///                    invalid_request
        /// The request is missing a required parameter, includes an
        /// unsupported parameter or parameter value, or is otherwise
        /// malformed.
        ///                    unauthorized_client
        /// The client is not authorized to request an authorization code
        /// using this method.
        ///                    access_denied
        /// The resource owner or authorization server denied the request.
        /// unsupported_response_type
        /// The authorization server does not support obtaining an
        /// authorization code using this method.
        ///                    invalid_scope
        /// The requested scope is invalid, unknown, or malformed.
        ///                    a 4xx or 5xx HTTP status code 
        ///                    (except for 400 and 401)
        /// [[ Pending Consensus ]] The authorization server MAY set the
        /// error parameter value to a numerical HTTP status code from
        /// the 4xx or 5xx range, with the exception of the 400 (Bad
        /// Request) and 401 (Unauthorized) status codes. For example, if
        /// the service is temporarily unavailable, the authorization server
        /// MAY return an error response with error set to 503.
        /// </summary>
        public string Error { get; set; }

        /// <summary>
        /// OPTIONAL. A human-readable text providing additional information, used to assist
        /// in the understanding and resolution of the error occurred.
        /// </summary>
        public string ErrorDescription { get; set; }

        /// <summary>
        /// OPTIONAL. A URI identifying a human-readable web page with information about
        /// the error, used to provide the resource owner with additional information about the
        /// error.
        /// </summary>
        public Uri ErrorUri { get; set; }

        /// <summary>
        /// REQUIRED if a valid state parameter was present in the client authorization
        /// request. Set to the exact value received from the client.
        /// </summary>
        public string state { get; set; }

        public IDictionary<string, object> ToResponseValues() { 

            Dictionary<string,object> response=new Dictionary<string,object>();
            response.Add(Parameters.ErrorParameters.Error, Error);
            response.Add(Parameters.ErrorParameters.ErrorDescription, ErrorDescription);
            response.Add(Parameters.ErrorParameters.ErrorUri, ErrorUri);

            return response;
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.Append("{");
            builder.AppendFormat(@"Error: ""{0}"",", Error??"");
            builder.AppendFormat(@"ErrorDescription: ""{0}"", ", ErrorDescription??"");
            builder.AppendFormat(@"ErrorUri: ""{0}""", ErrorUri==null?"":ErrorUri.ToString());
            builder.Append("}");
            return base.ToString();
        }
    }
}
